<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
namespace app\commands;

use Yii;
use Pheanstalk\Pheanstalk;
use yii\console\Controller;
use yii\console\ExitCode;
use app\controllers\BaseController;
use app\controllers\MorningController;
use app\models\GameUser;
use app\models\Morning;
use app\models\MorningRecord;

/**
 * This command echoes the first argument that you have entered.
 *
 * This command is provided as an example for you to learn how to create console commands.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class ConsumerController extends Controller
{
    /**
     * This command echoes what you have entered as the message.
     * @param string $message the message to be echoed.
     * @return int Exit code
     */
    public function actionIndex(){
        $pheanstalk = new Pheanstalk( '127.0.0.1', 11300 );
        while (true) {
            $job = $pheanstalk->watch('wakeup')->reserve( 5 );
            if($job){
                $jobData = $job->getData();
                $data = json_decode($jobData,true);

                $morningdata = array(//储存到morning表的数据
                    'userid' => $data['userid'], //用户序号
                    'signdays' => $data['signdays'], //签到天数
                    'onsign' => $data['onsign'],  //连续签到天数
                    'datetime' => $data['datetime'], //签到时间
                    'totalscore' => $data['totalscore'], //总积分
                    'shopscore' => $data['shopscore'],  //商城积分
                    'msectime' => $data['msectime'],
                );

                $recorddata = array(//储存到recored表的数据
                    'userid' => $data['userid'],
                    'time' => $data['datetime'],
                    'msectime' => $data['msectime'],
                    'score' => $data['score'],//获得积分
                    'signscore' => $data['signscore'],
                );
                
                $morning = Morning::signupUpdate($morningdata);
                $record = MorningRecord::add($recorddata);
                $pheanstalk->delete($job);
                //sleep适当使用 不然会造成签到不恰当
                //sleep(5);
            }
        }
    }


}
